//index.js
//获取应用实例
const app = getApp()
var util =  require('../../utils/util.js');

Page({
  data: {
    mainName: app.globalData.mainName,
    btn:{
      btnTxt: '个人主页',
      src: '/resources/index/whiteRight.png',
      imgW: '12'
    },
    mainItemList: [
      {
        url: app.globalData.apiUrl + '/Uploads/ce/indexIcon1.png',
        text: '电子围栏'
      },
      {
        url: app.globalData.apiUrl + '/Uploads/ce/indexIcon2.png',
        text: '一键呼救'
      },
      {
        url: app.globalData.apiUrl + '/Uploads/ce/indexIcon3.png',
        text: '智能家居监护'
      },
      {
        url: app.globalData.apiUrl + '/Uploads/ce/indexIcon4.png',
        text: '健康数据'
      },
    ]
  },
  onLoad: function () {
    util.setNavBarColor("#ffffff",app.globalData.themeColor);
  },

  //授权登录
  authorizedLoginBtn:function(){
    var that = this
    if(that.data.userInfo){
      //已授权
      wx.redirectTo({
        url: '/pages/login/login'
      })
    }else{
      let isLogin = that.data.userId?true:false
      //未授权
      wx.navigateTo({
        url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ isLogin,
      })
    }
  },

  //获取身份
  getIdentity:function(userId){
    var that = this;
    util.getAjax(app.globalData.apiUrl +'/yes_expert',function(res){
      if(res.data){
        app.globalData.identity.id = res.data.id
        app.globalData.identity.type = 'experts'
        util.getAjax(app.globalData.apiUrl +'/expert_get_communtiy_health_expert_tall',function(res){
          // console.log(res)
          if(res.data && res.data != 0){
            that.setData({
              isMessage: true,
              num: res.data
            })
            app.globalData.identity.num = res.data
          }else{
            that.setData({
              isMessage: false,
              num: 0
            })
          }
        },{expert_id: app.globalData.identity.id})
      }
    },{user_id: userId})
  },

  //获取社区电话
  getCommunityPhone:function(communityId){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/get_community_phone',function(res){
      if(res.data.length != 0){
        that.setData({
          isPhone: true,
          phoneList: res.data
        })
      }
    },{community_id: communityId})
  },

  //获取热门内容banner
  getSupermarketBanner:function(communityId){
    var that = this
    //超市促销banner
    util.getAjax(app.globalData.apiUrl+'/get_community_service_banner',function(res){
      // console.log(res);
      that.setData({
        isMarketImg: true,
        list:res.data
      })
    },{community_id:communityId})
  },

  //个人主页
  lickBtn:function(){
    var that = this
    wx.navigateTo({
      url: '/subPages/mine/personalCenter/personalCenter',
    })
  },

  //按钮
  mainBtn:function(e){
    // console.log(e)
    var that = this
    var index = e.currentTarget.dataset.index
    switch (index) {
      case 1:
        wx.navigateTo({
          url: '/subPages/pensionService/electronicFence/electronicFence',
        })
        break;
      case 2:
        wx.makePhoneCall({
          phoneNumber: '120'
        })
        break;
      case 3:
        // util.showToast('正在开发','loading',2000)
        wx.navigateTo({
          url: '/subPages/pensionService/homeCare/homeCare',
        })
        break;
      case 4:
        if(app.globalData.userId){
          wx.navigateTo({
            url: '/subPages/health/healthData/index/index',
          })
        }else{
          util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
            if (app.globalData.userInfo) {
              //已授权
              wx.redirectTo({
                url: '/pages/login/login'
              })
            }else{
              // 在没有 open-type=getUserInfo 版本的兼容处理
              if(app.globalData.isAuthorized){
                app.userInfoReadyCallback = res => {
                  if (res) {
                    //已授权
                    wx.redirectTo({
                      url: '/pages/login/login'
                    })
                  }else{
                    //未授权
                    wx.navigateTo({
                      url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                    })
                  }
                }
              }else{
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          })
        }
        break;
    }
  },

  //打电话
  phoneBtn:function(e){
    var phone = e.currentTarget.dataset.phone
    if(phone != '')
    {
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }else{
      util.showModal('提示','电话不存在')
      return
    }
  },

  //热门内容
  imgNavTo:function(e){
    var id = e.currentTarget.dataset.id
      // console.log(id);
    wx.navigateTo({
      url:'/subPages/communityService/PresaleSupermarket/PresaleSupermarket?id='+id
    })
  },

  onShow:function(){
    var that = this
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getIdentity(that.data.userId)
    }else{
      app.userIdCallback = userId => {
        if (userId != '' && userId != null) {
          that.setData({
            userId: userId
          });
          that.getIdentity(that.data.userId)
        }
      }
    }
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }else{
       // 在没有 open-type=getUserInfo 版本的兼容处理
       app.userInfoReadyCallback = res => {
        if (res) {
          that.setData({
            userInfo: res.userInfo
          });
        }
      }
    }

    //社区id
    if(app.globalData.communityId){
      that.setData({
        communityId: app.globalData.communityId
      })
      that.getCommunityPhone(app.globalData.communityId)
      that.getSupermarketBanner(app.globalData.communityId)
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '' && communityId != null) {
          that.setData({
            communityId: communityId
          })
          that.getCommunityPhone(communityId)
          that.getSupermarketBanner(communityId)
        }
      }
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    if(that.data.communityId){
      that.getCommunityPhone(that.data.communityId)
      that.getSupermarketBanner(that.data.communityId)
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})